package cn.anstone.service;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.anstone.mapper.UserMapper;
import cn.anstone.pojo.User;
import cn.anstone.pojo.UserExample;
import cn.anstone.pojo.UserExample.Criteria;

/**
 * @author J.L.Lee
 * @date  Aug 15, 2017
 */
@Service
public class UserService {

	@Autowired
	UserMapper userMapper;
	
	public List<User> getAllUser() {
		
		return userMapper.selectByExampleWithRole(null);
	}

	public boolean checkUser(String username) {
		
		UserExample example = new UserExample();
		
		Criteria criteria = example.createCriteria();
		
		criteria.andUsernameEqualTo(username);
		
		long count = userMapper.countByExample(example);
		
		return count == 0;
		
	}

	public void saveUser(User user) {
		
		//Random random = new Random();
		
		user.setId(111111);
		
		userMapper.insertSelective(user);
	}

	public User getUserById(Integer id) {
		
		User user = userMapper.selectByPrimaryKey(id);
		
		return user;
	}

	public void updateUser(User user) {
		
		userMapper.updateByPrimaryKeySelective(user);
		
	}
	
	/**
	 * 员工删除
	 * @param id
	 */
	public void deleteUserById(Integer id) {
		// TODO Auto-generated method stub
		userMapper.deleteByPrimaryKey(id);
	}

	public void deleteBatch(List<Integer> ids) {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		//delete from xxx where id in(1,2,3)
		
		criteria.andIdIn(ids);
		userMapper.deleteByExample(example);
	}

}
